package com.ql.controller.menzhen;

import com.ql.pojo.menzhen.Fomulary;
import com.ql.pojo.menzhen.User;
import com.ql.service.menzen.FomularyService;
import com.ql.service.menzen.UserLoginService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/menzhen/")
public class menzhenController {

    @Resource
    public FomularyService fomularyService;

    @Resource
    public UserLoginService userLoginService;

    @RequestMapping("chufang")
    public String chufang(@RequestParam("name") Integer uId, Model model){
        List<Fomulary> fomularyList = fomularyService.getById(uId);
        User user = userLoginService.getUser(uId);
        model.addAttribute("uId",uId);
        model.addAttribute("huanzhe",user);
        model.addAttribute("fomularyList",fomularyList);
        return "menzhenbingli";
    }

    @RequestMapping("addchufang")
    public String addchufang(@ModelAttribute("fomulary") Fomulary fomulary,Model model){
        System.out.println(fomulary.toString());
        boolean bl = fomularyService.addFomulary(fomulary);
        if(bl){
            model.addAttribute("add","添加成功");
            model.addAttribute("fomulary",fomulary);
            return "menzhenbingli";
        }else {
            model.addAttribute("add","添加失败");
            model.addAttribute("fomulary",fomulary);
            return "menzhenbingli";
        }
    }

    @RequestMapping("binli")
    public String binli(@Param("fId")int fId ,Model model){
        Fomulary fom = fomularyService.getFid(fId);
        model.addAttribute("fomulary",fom);
        return "binglixiangqing";
    }

    @RequestMapping("jiancha")
    public String jiancha(@RequestParam("uId") Integer uId, Model model){
        model.addAttribute("uId",uId);
        return "menzhenjiancha";
    }

}
